package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
@RequestMapping("/menu")
public class MenuController {

    @Reference
    MenuService menuService;

    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        log.debug("查询条件：" + queryPageBean);
        PageResult pageResult = menuService.findPage(queryPageBean);
        log.debug("分页查询结果：" + pageResult.getRows());
        return new Result(true, "查询菜单成功", pageResult);
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu) {
        log.debug("添加方法执行，请求体为：" + menu);
        menuService.add(menu);
        return new Result(true, "添加菜单成功");
    }

    @RequestMapping("/findById/{id}")
    public Result findById(@PathVariable("id") Integer id) {
        log.debug("findById方法执行：id" + id);
        Menu menu = menuService.findById(id);
        return new Result(true, "根据id查询菜单完成", menu);
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Menu menu) {
        log.debug("edit方法执行:" + menu);
        menuService.edit(menu);
        return new Result(true, "编辑菜单成功");
    }

    @RequestMapping("/delById/{id}")
    public Result delById(@PathVariable("id") Integer id) {
        log.debug("delById方法执行 id = " + id);
        menuService.delMenuById(id);
        return new Result(true, "删除完成");
    }

}